package com.jml.注册校验器;

import org.springframework.stereotype.Component;
import org.springframework.validation.Errors;
import org.springframework.validation.Validator;

@Component
public class User参数校验Validator implements Validator {

    /**
     * 使用校验的时候先会判断是否支持这个类
     */
    @Override
    public boolean supports(Class<?> clazz) {
        return User.class.equals(clazz);
    }

    @Override
    public void validate(Object obj, Errors errors) {
        User user = (User)obj;
        if(user==null||user.getUserName()==null||user.getUserName().length()<5){
            errors.rejectValue("userName",
                    "错误码：用户名长度",
                    new Object[]{"最小长度",5},
                    "用户名不能小于{1}位");
        }
    }
}
